package com.hoperun.checkcloth.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hoperun.checkcloth.databinding.ItemFlawTypeBinding
import com.hoperun.checkcloth.net.DictItem

class BoltFlawRemarkAdapter(
        private val itemClickListener: ((DictItem) -> Unit)? = null
) : RecyclerView.Adapter<BoltFlawRemarkVH>() {
    private val names: MutableList<DictItem> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BoltFlawRemarkVH {
        val binding = ItemFlawTypeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return BoltFlawRemarkVH(binding)
    }

    override fun onBindViewHolder(holder: BoltFlawRemarkVH, position: Int) {
        holder.binding.name = names[position].value
        holder.itemView.setOnClickListener {
            itemClickListener?.apply { invoke(names[position]) }
        }
    }

    override fun getItemCount() = names.size

    fun setDataSet(list: List<DictItem>) {
        names.clear()
        names.addAll(list)
        notifyDataSetChanged()
    }
}

class BoltFlawRemarkVH(val binding: ItemFlawTypeBinding) : RecyclerView.ViewHolder(binding.root)
